package client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import project.controllers.ConsoleScanner;
import project.controllers.Printer;
import project.menu.Builder;
import project.menu.IMenu;
import project.menu.IMenuItem;
import project.menu.mainmenu.MainMenu;
import api.messages.Messager;

public class Client {

	private static Integer choice;
	private static IMenu currentMenu;
	private static IMenuItem currentMenuItem;
	private static MainMenu mainMenu;
	private static ConsoleScanner console_scanner;
	private static ObjectOutputStream objectWriter;
	private static ObjectInputStream objectReader;
	private static ArrayList<String> clienRequest;
	private static Logger log = Logger.getLogger(Client.class);

	public static void main(String[] args) {
		try {
			mainMenu = MainMenu.getInstance();
			console_scanner = ConsoleScanner.getInstance();
			ArrayList<String> serverAnswer = new ArrayList<String>();

			Socket s = new Socket(InetAddress.getLocalHost(), 8071);
			objectWriter = new ObjectOutputStream(s.getOutputStream());
			objectReader = new ObjectInputStream(s.getInputStream());
			Builder.build();
			Printer.printMenu(mainMenu.getMenu());
			currentMenu = mainMenu;

			Printer.printMessage(Messager.MAKE_CHOICE);

			do {
				try {
					choice = console_scanner.getChoice();
					if (choice > (currentMenu.getMenu().size() - 1)) {
						Printer.printMessage(Messager.ERROR_CHOICE);
					}

					for (IMenuItem i : currentMenu.getMenu()) {
						if (choice == i.getId()) {
							currentMenuItem = i;

							currentMenu = i.getMenu();
							clienRequest = i.action();

							if (clienRequest != null) {
								ArrayList<String> tempClientRequest = new ArrayList<String>();
								tempClientRequest.addAll(clienRequest);
								objectWriter.writeObject(tempClientRequest);
								serverAnswer
										.addAll((ArrayList<String>) objectReader
												.readObject());
								for (String line : serverAnswer) {
									Printer.printMessage(line);
								}
								clienRequest.clear();
								serverAnswer.clear();

							}
							if (currentMenu != null) {
								Printer.printMessage(Messager.MAKE_CHOICE);
								Printer.printMenu(currentMenu.getMenu());
								
							}

						}
					}
				} catch (ClassNotFoundException e) {
					Printer.printMessage(Messager.EXCEPTION_SERVER_THREAD);
					log.error(Messager.EXCEPTION_SERVER_THREAD);

				}

			} while (currentMenuItem != mainMenu.getMenu().get(3));

			Printer.printMessage(Messager.THANKS);

			Thread.sleep(1000);
			objectReader.close();
			objectWriter.close();

			s.close();
		} catch (UnknownHostException e) {
			Printer.printMessage(Messager._404_);
			e.printStackTrace();
			log.error(Messager._404_);

		} catch (IOException e) {
			Printer.printMessage(Messager.IO_EXCEPTION);
			e.printStackTrace();
			log.error(Messager.IO_EXCEPTION);
		} catch (InterruptedException e) {

			e.printStackTrace();
			log.error(e);
		}
	}

}
